home *** CD-ROM | disk | FTP | other *** search
/ BMUG Revelations / BMUG Revelations.toast / Programming / Programming Languages / UCB Logo 3.0 / sources / standard source / ztcterm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-14  |  13.0 KB  |  608 lines  |  [TEXT/JV01]

  1. /*
  2.  *      ztcterm.c           IBM screen module             mak
  3.  *
  4.  *    Copyright (C) 1993 by the Regents of the University of California
  5.  *
  6.  *      This program is free software; you can redistribute it and/or modify
  7.  *      it under the terms of the GNU General Public License as published by
  8.  *      the Free Software Foundation; either version 2 of the License, or
  9.  *      (at your option) any later version.
  10.  *  
  11.  *      This program is distributed in the hope that it will be useful,
  12.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *      GNU General Public License for more details.
  15.  *  
  16.  *      You should have received a copy of the GNU General Public License
  17.  *      along with this program; if not, write to the Free Software
  18.  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  */
  21.  
  22. #include "logo.h"
  23. #include "globals.h"
  24. #include <math.h>
  25. #include <bios.h>
  26. #include <fg.h>
  27. #include <conio.h>
  28. #include <disp.h>
  29. #include <sound.h>
  30. #include "ztcterm.h"
  31.  
  32. extern x_margin, y_margin;
  33. /************************************************************/
  34.  
  35. unsigned _stack = 8000; /* 5000 for debugging, 65000 for real */
  36.  
  37. BOOLEAN in_graphics_mode = FALSE, in_splitscreen = FALSE;
  38. BOOLEAN have_been_in_graphics_mode = FALSE;
  39. BOOLEAN in_erase_mode = FALSE;
  40.  
  41. /* NOTE NOTE NOTE:  graphics.c really really believes that the top left
  42.  * corner of the screen has hardware coords (0,0).  Zortech has (0,0)
  43.  * in the BOTTOM left.  It's hopeless to fix graphics.c so instead we
  44.  * use the following kludge: go ahead and think the screen is upside down,
  45.  * and switch to Zortech coords only in draw_line! */
  46.  
  47. int ibm_screen_bottom, ibm_turtle_bottom_max;
  48. int current_write_mode = FG_MODE_SET;
  49. int current_vis = 0;
  50. fg_color_t turtle_color, bg_color;
  51. int texth;
  52. int MaxX, MaxY;
  53. int ztc_penwidth = 1, ztc_linepattern = -1;
  54. fg_box_t ztc_box, ztc_textbox, text_scroll_box, text_last_line_box, clear_box;
  55. void erase_graphics_top();
  56.  
  57. fg_line_t the_line;
  58. fg_coord_t ztc_x, ztc_y;
  59. fg_color_t ztc_textcolor;
  60. fg_coord_t ztc_graph_textx, ztc_graph_texty;
  61. FIXNUM pen_color = 0, back_ground = 7;
  62.  
  63. void moveto(int x, int y) {
  64.     ztc_x = (fg_coord_t)x;
  65.     ztc_y = (fg_coord_t)y;
  66. }
  67.  
  68. void lineto(int x, int y) {
  69.     fg_make_line(the_line, ztc_x, MaxY-ztc_y,
  70.              (fg_coord_t)x, MaxY-(fg_coord_t)y);
  71.     fg_drawlineclip((in_erase_mode ? bg_color : turtle_color),
  72.            current_write_mode, ~0, FG_LINE_SOLID, the_line, ztc_box);
  73.     moveto(x, y);
  74. }
  75.  
  76. void outtext(char *s) {
  77.     fg_puts(FG_WHITE, FG_MODE_SET, ~0, FG_ROT0, ztc_x, MaxY-ztc_y,
  78.             s, fg.displaybox);
  79. }
  80.  
  81. void gr_mode()
  82. {
  83.     int errorcode;
  84.  
  85.     if (!in_graphics_mode) {
  86.     x_coord = x_margin;
  87.     y_coord = y_margin;
  88.     errorcode = fg_init();
  89.     if (have_been_in_graphics_mode) {
  90.         in_graphics_mode = TRUE;
  91.         redraw_graphics();
  92.     }
  93.     else {
  94.         if (errorcode == 0)
  95.         err_logo(BAD_GRAPH_INIT, NIL);
  96.         else {
  97.         in_graphics_mode = have_been_in_graphics_mode = TRUE;
  98.         pen_color = 7;
  99.         turtle_color = FG_HIGHLIGHT;
  100.         back_ground = 0;
  101.         bg_color = FG_BLACK;
  102.         ztc_textcolor = FG_WHITE;
  103.         ztc_box[FG_X1] = ztc_textbox[FG_X1]
  104.                    = text_scroll_box[FG_X1]
  105.                    = text_last_line_box[FG_X1]
  106.                    = clear_box[FG_X1]
  107.                    = fg.displaybox[FG_X1];
  108.         ztc_textbox[FG_Y1] = fg.displaybox[FG_Y1];
  109.         ztc_box[FG_X2] = ztc_textbox[FG_X2]
  110.                    = text_scroll_box[FG_X2]
  111.                    = text_last_line_box[FG_X2]
  112.                    = clear_box[FG_X2]
  113.                    = MaxX = fg.displaybox[FG_X2];
  114.         ztc_box[FG_Y2] = MaxY
  115.                    = clear_box[FG_Y2]
  116.                    = fg.displaybox[FG_Y2];
  117.         y_scale = (double)fg.pixelx/(double)fg.pixely;
  118.         {
  119.             FILE *fp = fopen("scrunch.dat","r");
  120.             if (fp != NULL) {
  121.             fread(&x_scale, sizeof(FLONUM), 1, fp);
  122.             fread(&y_scale, sizeof(FLONUM), 1, fp);
  123.             fclose(fp);
  124.             }
  125.         }
  126.         if (MaxY == 479)
  127.             texth = 16;
  128.         else
  129.             texth = (MaxY+1)/25;
  130.         ztc_box[FG_Y1] = 4*texth+1;
  131.         clear_box[FG_Y1] = 4*texth;
  132.         ibm_screen_bottom = MaxY - (ztc_box[FG_Y1]);
  133.         ztc_textbox[FG_Y2] = 4*texth-1;
  134.         text_scroll_box[FG_Y2] = 3*texth-1;
  135.         text_scroll_box[FG_Y1] = 0;
  136.         text_last_line_box[FG_Y2] = texth-1;
  137.         ibm_turtle_bottom_max = -(MaxY/2) + (4*texth);
  138.         lclearscreen();
  139.         lcleartext();
  140.         in_splitscreen = TRUE;
  141.        }
  142.     }
  143.     }
  144. }
  145.  
  146. void nop()
  147. {
  148. }
  149.  
  150. void init_ibm_memory()
  151. {
  152. }
  153.  
  154. volatile int ctrl_c_count = 0;
  155.  
  156. BOOLEAN check_ibm_stop()
  157. {
  158.     int key;
  159.     int __ss *p;
  160.  
  161. /*    if (kbhit())
  162.     getch(); */   /* allow DOS to test for control-C */
  163.  
  164.     p = &key;
  165.     if ((int)p < 500) {
  166.     err_logo(STACK_OVERFLOW, NIL);
  167.     return(1);
  168.     }
  169.  
  170.     if (((key = bioskey(1)) == (4113)) || ctrl_c_count > 0) { /* control-q */
  171.     if (ctrl_c_count == 0) getch();
  172.     ctrl_c_count = 0;
  173.     err_logo(STOP_ERROR,NIL);
  174.     return(1);
  175.     }
  176.     if (key == (4375)) { /* control-w */
  177.     getch();
  178.     to_pending = 0;
  179.     lpause();
  180.     }
  181.     return (0);
  182. }
  183.  
  184. void term_init_ibm()
  185. {
  186.     disp_open();
  187.     ztc_textcolor = FG_WHITE;
  188.     tty_charmode = 0;
  189.     x_max = 80;
  190.     y_max = 24;
  191.     x_coord = y_coord = 0;
  192.     so_arr[0] = '\1'; so_arr[1] = '\0';
  193.     se_arr[0] = '\2'; se_arr[1] = '\0';
  194. }
  195.  
  196. void ibm_gotoxy(int x, int y)
  197. {
  198.     disp_move(y, x);
  199.     disp_flush();
  200. }
  201.  
  202. void ibm_clear_text() {
  203.     if (in_graphics_mode) {
  204.     if (ibm_screen_bottom != 0)
  205.         erase_graphics_top();
  206.     } else {
  207.     disp_move(0,0);
  208.     disp_eeop();
  209.     disp_flush();
  210.     }
  211. }
  212.  
  213. void ibm_clear_screen()
  214. {
  215.     fg_fillbox(bg_color, FG_MODE_SET, ~0, clear_box);
  216. }
  217.  
  218. void ibm_plain_mode()
  219. {
  220.     if (in_graphics_mode)
  221.     ztc_textcolor = FG_WHITE;
  222.     else
  223.     disp_endstand();
  224. }
  225.  
  226. void ibm_bold_mode()
  227. {
  228.     if (in_graphics_mode)
  229.     ztc_textcolor = FG_HIGHLIGHT;
  230.     else
  231.     disp_startstand();
  232. }
  233.  
  234. void erase_graphics_top()
  235. {
  236.     fg_fillbox(FG_BLACK, FG_MODE_SET, ~0, ztc_textbox);
  237.     ztc_graph_textx = 0;
  238.     ztc_graph_texty = 3*texth;
  239. }
  240.  
  241. /************************************************************/
  242. /* These are the machine-specific graphics definitions.  All versions must
  243.    provide a set of functions analogous to these. */
  244.  
  245. void save_pen(p)
  246. pen_info *p;
  247. {
  248.     p->h = ztc_x;
  249.     p->v = ztc_y;
  250.     p->vis = current_vis;
  251.     p->width = ztc_penwidth;
  252.     p->color = turtle_color;
  253.     get_pen_pattern(p->pattern);
  254.     p->mode = get_ibm_pen_mode();
  255. }
  256.  
  257. void restore_pen(p)
  258. pen_info *p;
  259. {
  260.     moveto(p->h, p->v);
  261.     current_vis = p->vis;
  262.     ztc_penwidth = p->width;
  263.     set_ibm_pen_mode(p->mode);  /* must restore mode before color */
  264.     turtle_color = p->color;
  265.     set_pen_pattern(p->pattern);
  266. }
  267.  
  268. fg_color_t color_table[8] = {0, 9, 10, 11, 12, 13, 14, 15};
  269.  
  270. FIXNUM hw_color(FIXNUM c) {
  271.     if (c >= 0 && c < 8) return color_table[c];
  272.     if (c < 0) return c;
  273.     return c-8;
  274. }
  275.  
  276. void plain_xor_pen()
  277. {
  278.     ztc_penwidth = 1;
  279.     turtle_color = hw_color(pen_color)^bg_color;
  280.     current_write_mode = FG_MODE_XOR;
  281.     in_erase_mode = FALSE;
  282. }
  283.  
  284. void ztc_set_penc(FIXNUM c)
  285. {
  286.     draw_turtle();
  287.     pen_color = c;
  288.     turtle_color = hw_color(c);
  289.     if (current_write_mode == FG_MODE_XOR)
  290.     turtle_color = hw_color(pen_color)^bg_color;
  291.     draw_turtle();
  292. }
  293.  
  294. void ztc_set_bg(FIXNUM c) {
  295.     back_ground = c;
  296.     bg_color = hw_color(c);
  297.     redraw_graphics();
  298. }
  299.  
  300. void ibm_pen_down()
  301. {
  302.     current_write_mode = FG_MODE_SET;
  303.     turtle_color = hw_color(pen_color);
  304.     in_erase_mode = FALSE;
  305. }
  306.  
  307. void ibm_pen_xor()
  308. {
  309.     current_write_mode = FG_MODE_XOR;
  310.     turtle_color = hw_color(pen_color)^bg_color;
  311.     in_erase_mode = FALSE;
  312. }
  313.  
  314. void ibm_pen_erase()
  315. {
  316.     if (!in_erase_mode) {
  317.     current_write_mode = FG_MODE_SET;
  318.     turtle_color = hw_color(pen_color);
  319.     in_erase_mode = TRUE;
  320.     }
  321. }
  322.  
  323. int get_ibm_pen_mode()
  324. {
  325.     if (in_erase_mode)
  326.     return 2;
  327.     else
  328.     return current_write_mode;
  329. }
  330.  
  331. void set_ibm_pen_mode(int m)
  332. {
  333.     switch (m) {
  334.     case 2: ibm_pen_erase(); break;
  335.     case FG_MODE_SET: ibm_pen_down(); break;
  336.     case FG_MODE_XOR: ibm_pen_xor(); break;
  337.     }
  338. }
  339.  
  340. int get_ibm_pen_width()
  341. {
  342.     return ztc_penwidth;
  343. }
  344.  
  345. void set_pen_pattern(char *pat)
  346. {
  347.     ztc_linepattern = *(int *)pat;
  348. }
  349.  
  350. void set_list_pen_pattern(NODE *arg)
  351. {
  352.     NODE *temp;
  353.  
  354.     temp = cnv_node_to_numnode(car(arg));
  355.     ztc_linepattern = getint(temp);
  356.     gcref(temp);
  357. }
  358.  
  359. void get_pen_pattern(char *pat)
  360. {
  361.     *(int *)pat = ztc_linepattern;
  362. }
  363.  
  364. NODE *Get_node_pen_pattern()
  365. {
  366.     return(cons(make_intnode(-1)), NIL);
  367. }
  368.  
  369. NODE *Get_node_pen_mode()
  370. {
  371.     if (in_erase_mode)
  372.     return(make_static_strnode("erase"));
  373.     if (current_write_mode == FG_MODE_XOR)
  374.     return(make_static_strnode("reverse"));
  375.     return(make_static_strnode("paint"));
  376. }
  377.  
  378. void label(char *s)
  379. {
  380.     gr_mode();
  381.     moveto(g_round(screen_x_coord), g_round(screen_y_coord));
  382.     outtext(s);
  383. }
  384.  
  385. void logofill()
  386. {
  387.     fg_fill(ztc_x, MaxY-ztc_y, turtle_color, turtle_color);
  388. }
  389.  
  390. void erase_screen()
  391. {
  392.     ibm_clear_screen();
  393. }
  394.  
  395. void t_screen()
  396. {
  397.     if (in_graphics_mode) {
  398.     fg_term();
  399.     in_graphics_mode = FALSE;
  400.     in_splitscreen = FALSE;
  401.     }
  402. }
  403.  
  404. void s_screen()
  405. {
  406.     int save_vis;
  407.  
  408.     ibm_turtle_bottom_max = -(MaxY/2) + (4*texth);
  409.     if (in_graphics_mode && !in_splitscreen) {
  410.     if (turtle_shown) {
  411.         save_vis = current_vis;
  412.         current_vis = -1;
  413.         lhome();
  414.         current_vis = save_vis;
  415.     }
  416.     erase_graphics_top();
  417.     }
  418.     in_splitscreen = TRUE;
  419.     ztc_box[FG_Y1] = 4*texth+1;
  420.     clear_box[FG_Y1] = 4*texth;
  421.     ibm_screen_bottom = MaxY - (ztc_box[FG_Y1]);
  422.     gr_mode();
  423. }
  424.  
  425. void f_screen()
  426. {
  427.     if (in_graphics_mode && in_splitscreen)
  428.     erase_graphics_top();
  429.     in_splitscreen = FALSE;
  430.     ztc_box[FG_Y1] = clear_box[FG_Y1] = 0;
  431.     ibm_screen_bottom = MaxY;
  432.     ibm_turtle_bottom_max = -(MaxY/2);
  433.     gr_mode();
  434. }
  435.  
  436. FIXNUM mickey_x()
  437. {
  438.     return 0;
  439. }
  440.  
  441. FIXNUM mickey_y()
  442. {
  443.     return 0;
  444. }
  445.  
  446. BOOLEAN Button()
  447. {
  448.     return FALSE;
  449. }
  450.  
  451. void tone(pitch, duration)
  452. FIXNUM pitch, duration;
  453. {
  454.     FIXNUM period = 200000L/pitch;
  455.     FIXNUM cycles = pitch/60;
  456.     sound_tone((int)cycles, (int)period, (int)period);
  457. }
  458.  
  459. FIXNUM t_height()
  460. {
  461.     return 18;
  462. }
  463.  
  464. FLONUM t_half_bottom()
  465. {
  466.     return 6.0;
  467. }
  468.  
  469. FLONUM t_side()
  470. {
  471.     return 19.0;
  472. }
  473.  
  474. void check_scroll(void)
  475. {
  476.     fg_blit(text_scroll_box, 0, texth, fg.activepage, fg.activepage);
  477.     fg_fillbox(FG_BLACK, FG_MODE_SET, ~0, text_last_line_box);
  478. }
  479.  
  480. void ztc_put_char(int ch) {
  481.     if (in_graphics_mode && !in_splitscreen)
  482.     lsplitscreen();
  483.     if (ch != '\1' && ch != '\2') {
  484.     if (in_graphics_mode) {
  485.         if (ch == '\n') {
  486.         ztc_graph_textx = 0;
  487.         if (ztc_graph_texty - texth >= 0)
  488.             ztc_graph_texty -= texth;
  489.         else check_scroll();
  490.         } else if (ch == '\b') {
  491.         if (ztc_graph_textx > 0)
  492.             fg_adjustxy(FG_ROT180, 1, &ztc_graph_textx,
  493.                 &ztc_graph_texty, fg.charbox);
  494.         } else if (ch == '\t') {
  495.         fg_adjustxy(FG_ROT0, 8 - (x_coord&07),
  496.                 &ztc_graph_textx,
  497.                 &ztc_graph_texty, fg.charbox);
  498.         if (ztc_graph_textx >= MaxX) print_char(stdout,'\n');
  499.         } else {
  500.         fg_putc(ztc_textcolor, FG_MODE_SET, ~0, FG_ROT0,
  501.             ztc_graph_textx, ztc_graph_texty,
  502.             ch, fg.displaybox);
  503.         fg_adjustxy(FG_ROT0, 1, &ztc_graph_textx,
  504.                 &ztc_graph_texty, fg.charbox);
  505.         if (ztc_graph_textx >= MaxX) print_char(stdout,'\n');
  506.         }
  507.     } else
  508.             disp_putc(ch);
  509.     }
  510. }
  511.  
  512. BOOLEAN cursor_off = 0;
  513.  
  514. void fix_cursor(void) {
  515.     if (!cursor_off && !in_graphics_mode) {
  516.         disp_hidecursor();
  517.     cursor_off++;
  518.     }
  519. }
  520.  
  521. void zflush(void) {
  522.     if (in_graphics_mode)
  523.     fg_flush();
  524.     else {
  525.     disp_flush();
  526.     if (cursor_off) disp_showcursor();
  527.     cursor_off = 0;
  528.     }
  529. }
  530.  
  531. void newline_bugfix(void) {
  532.     if (!in_graphics_mode) {
  533.     if (disp_cursorrow+1 < disp_numrows)
  534.             new_line(stdout);
  535.     else
  536.         disp_move(disp_numrows-1,0);
  537.     disp_flush();
  538.     }
  539. }
  540.  
  541. char graph_line_buffer[200];
  542. char *graph_line_pointer = NULL;
  543.  
  544. int ztc_getc(FILE *strm) {
  545.     int ch;
  546.  
  547.     if (strm != stdin || !interactive || !in_graphics_mode)
  548.     return getc(strm);
  549.     if (graph_line_pointer != NULL) {
  550.     ch = *graph_line_pointer++;
  551.     if (ch == '\t') graph_line_pointer += 2;
  552.     if (ch != '\0') return ch;
  553.     }
  554.     graph_line_pointer = graph_line_buffer;
  555.     do {
  556.     fg_putc(FG_WHITE, FG_MODE_XOR, ~0, FG_ROT0,
  557.         ztc_graph_textx, ztc_graph_texty,
  558.         '_', fg.displaybox);
  559.     ch = getch();
  560.     fg_putc(FG_WHITE, FG_MODE_XOR, ~0, FG_ROT0,
  561.         ztc_graph_textx, ztc_graph_texty,
  562.         '_', fg.displaybox);
  563.     if (ch == '\r' || ch == '\n') {
  564.         ch = '\n';
  565.         *graph_line_pointer++ = ch;
  566.         print_char(stdout,'\n');
  567.         *graph_line_pointer = '\0';
  568.         graph_line_pointer = graph_line_buffer;
  569.         return *graph_line_pointer++;
  570.     } else if (ch == '\t') {
  571.         int i = 8 - (x_coord & 07);
  572.         *graph_line_pointer++ = '\t';
  573.         *graph_line_pointer++ = (char)i;
  574.         *graph_line_pointer++ = '\t';
  575.         print_char(stdout, '\t');
  576.     } else if (ch == 0177 || ch == '\b') {
  577.         if (graph_line_pointer != graph_line_buffer) {
  578.         graph_line_pointer--;
  579.         if (*graph_line_pointer == '\t') {
  580.             int i = (int)*--graph_line_pointer;
  581.             --graph_line_pointer;
  582.             while (i-- > 0) print_char(stdout, '\b');
  583.         } else {
  584.             print_char(stdout, '\b');
  585.             ztc_textcolor = FG_BLACK;
  586.             print_char(stdout, *graph_line_pointer);
  587.             ztc_textcolor = FG_WHITE;
  588.             print_char(stdout, '\b');
  589.         }
  590.         }
  591.     } else if (ch == 17 || ch == 23) {
  592.         print_char(stdout,'\n');
  593.         graph_line_pointer = NULL;
  594.         return ch;
  595.     } else {
  596.         *graph_line_pointer++ = ch;
  597.         print_char(stdout, ch);
  598.     }
  599.     } while (ch != '\n');
  600.     graph_line_pointer = graph_line_buffer;
  601.     return *graph_line_pointer++;
  602. }
  603.  
  604. void ztc_getcr(void) {
  605.     (void) ztc_getc(stdin);
  606.     graph_line_pointer = NULL;
  607. }
  608.